1810C - Make It Permutation - CodeForces Solution


greedy sortings

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define len(s) s.size()
#define ull unsigned long long
#define ll long long
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define nl '\n'
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define dbg(x) cout << #x << " " << x << nl
#define mod 1000000007
#define md 998244353
#define ld long double


  
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define arrayin(a, n)           \
    int a[n];                   \
    for (int i = 0; i < n; i++) \
    cin >> a[i]

void Print(int32_t x)
{
    cout << x << " ";
}
void Print(char x) { cout << x << " "; }
void Print(double x) { cout << x << " "; }
void Print(int64_t x) { cout << x << " "; }
void print() { cout << endl; }
void Print(bool x) { cout << (x ? "YES" : "NO"); }
void Print(string x) { cout << x << " "; }
template <typename T, typename V>
void Print(const pair<T, V> x)
{
    cout << x.first << " " << x.second << endl;
}
template <typename T>
void Print(const T &x)
{
    for (auto &i : x)
        Print(i);
}
template <typename T, typename... V>
void print(T t, V... v)
{
    Print(t);
    print(v...);
}

ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    if (b == 1)
        return 1;
    else
        return gcd(b, a % b);
}

vector<char> vwl = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};

bool vowel(char a)
{
    for (auto x : vwl)
        if (a == x)
            return true;
    return false;
}

ll root(ll x)
{
    ll ans;
    if (x == 1)
        return 1;
    ll s = 1, e = INT_MAX / 2, mid;
    while (s <= e)
    {
        mid = (s + e) >> 1;
        if (mid * mid <= x)
        {
            ans = mid;
            s = mid + 1;
        }
        else
            e = mid - 1;
    }
    return ans;
}

bool isPrime(ll x)
{
    if (x == 1)
        return 0;
    for (ll i = 2; i * i <= x; i++)
    {
        if (x % i == 0)
            return false;
    }
    return true;
}

ll power(ll a, ll b)
{
    if (a <= 0 || b < 0)
        return 0;
    if (b == 0)
        return 1;
    if (b % 2)
        return (a * power(a, b - 1));
    else
        return power((a * a), b / 2);
}

int sod(int x)
{
    int ans = 0;
    while (x)
    {
        ans += x % 10;
        x /= 10;
    }
    return ans;
}

int nod(int x)
{
    int ans = 0;
    while (x)
    {
        ans++;
        x /= 10;
    }
    return ans;
}

ll mod_power(ll a, ll b)
{

    if (b == 0)
        return 1;
    if (b == 1)
        return a;
    if (b % 2)
        return (a % mod * mod_power(a, b - 1) % mod) % mod;
    else
        return mod_power((a % mod * a % mod) % mod, b / 2) % mod;
}

ll max(ll a, ll b)
{
    return a > b ? a : b;
}

ll min(ll a, ll b)
{
    return a < b ? a : b;
}

bool multitest = 1;

void solve()
{
  int n,c,d;
  cin>>n>>c>>d;
  arrayin(a,n);
  sort(a,a+n);
  ordered_set s;
  for(int i =0;i<n;i++){
    s.insert(a[i]);
  }
  int ans = 1e18;
  for(int i = 1;i<=n;i++){
    int x = s.order_of_key(i+1);
    ans = min(ans,(n-x)*c+max(0,i-x)*d);
  }
  for(int i = 0;i<n;i++){
    int x = s.order_of_key(a[i]+1);
    ans = min(ans,(n-x)*c+max(0,a[i]-x)*d);
  }
  cout<<ans<<nl;

}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int tc = 1;
    if (multitest)
        cin >> tc;
    for (int tt = 1; tt <= tc; tt++)
    {
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves